#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define maxn 1000002
struct node
{
    int val, pos;
    node(int val = 0, int pos = 0) : val(val), pos(pos){};
};
int arr[maxn];
node que[maxn];
int n, k;
void getMinQue()
{
    int front = 0, tail = 0;
    for (int i = 1; i <= n; ++i)
    {
        while (front <= tail && arr[i] <= que[tail].val)
            tail--;
        que[++tail] = node(arr[i], i);
        while (front <= tail && que[front].pos + k <= i)
            front++;
        if (i >= k)
            printf("%d ", que[front].val); //输出队头元素
    }
}
void getMaxQue()
{
    int front = 0, tail = 0;
    for (int i = 1; i <= n; ++i)
    {
        while (front <= tail && arr[i] >= que[tail].val)
            tail--;
        que[++tail] = node(arr[i], i);
        while (front <= tail && que[front].pos + k <= i)
            front++;
        if (i >= k)
            printf("%d ", que[front].val); //输出队头元素
    }
}
int main()
{
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; ++i)
        scanf("%d", &arr[i]);
    getMinQue();
    printf("\n");
    getMaxQue();
}